package com.liang.demo.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/1 10:21
 */
// 工具类，通过 bean名称 获取对应的对象
@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //根据 bean 的名字 获取指定的bean对象
    //bean 有个 默认策略，类名首字母小写
    public static Object getBean(String BeanName){
        Object bean = context.getBean(BeanName);
        return bean;
    }
}